package com.quark.web.ui.field;

import java.io.File;
import com.quark.data.type.QuarkPair;
import com.quark.framework.base.QuarkEnvironment;
import com.quark.framework.database.QuarkDBExecuteManager;
import com.quark.framework.database.QuarkDBExecuterFactory;
import com.quark.framework.ormapping.QuarkOrmEntity;
import com.quark.framework.utility.QuarkLog;
import com.quark.framework.utility.QuarkUtilStringTools;
import com.quark.web.ui.base.QuarkUiInputElement;
import com.quark.web.ui.base.QuarkUiSetting;
import com.quark.web.ui.base.QuarkUiTableElement;
import com.quark.web.ui.base.QuarkUiTrElement;
import com.quark.web.ui.logic.QuarkUiManager;

public class QuarkUiSingleUploadField extends QuarkUiFieldBase {

	private QuarkUiInputElement element = null;
	public boolean isEnablePreview = false;
	public String previewImage = "";
	public QuarkUiSingleUploadField(String fieldName) {
		super(fieldName, new QuarkUiInputElement(fieldName, fieldName, QuarkUiSetting.SINGLE_UPLOAD_CLASS, ""));
		element = (QuarkUiInputElement) elementBase;
		element.addAttribute("type", "file");
		//TODO Add a attribute for js
		// TODO Auto-generated constructor stub
	}

	public QuarkUiSingleUploadField(String fieldName, boolean enablePreview) {
		super(fieldName, new QuarkUiInputElement(fieldName, fieldName, QuarkUiSetting.SINGLE_UPLOAD_CLASS, ""));
		element = (QuarkUiInputElement) elementBase;
		element.addAttribute("type", "file");
		isEnablePreview = enablePreview;
		//TODO Add a attribute for js
		// TODO Auto-generated constructor stub
	}
	
	public void clearField()
	{
		element.clearValue();
	}
	
	public void enablePreview(boolean willyou)
	{
		isEnablePreview = willyou;
	}
	
	public boolean fillFieldWith(Object data)
	//Accept String
	{
		if(data != null)
		{
			if(!data.getClass().equals(String.class))
			{
				QuarkLog.logError(getClass().getName(), "can't fill with " + data.getClass().getName() + ", " + String.class.getName() + " expected");
				return false;
			}
			if(isEnablePreview)
				previewImage = data.toString();
			else
				element.setValue(data.toString());
			return true;
		}
		else
		{
			QuarkLog.logError(getClass().getName(), "can't fill field with null");
			return false;
		}
	}
	
	public Object processSubmitData(Object rawData)
	{
		// this should be a file name
		return rawData.toString();
	}
	
	public String createField()
	{
		String additional = "";
		if(isEnablePreview)
		{
			additional = "<IMG imgsize='width:320,height:360' src='" + previewImage + "' width='auto' valign='top'/>";
		}
		return (QuarkUtilStringTools.isNullOrEmpty(previewImage) ? "" : additional) + element.createElement();
	}
	
	//utility
	public static boolean replaceEntitySingleUploadFile(String submitType, Class<? extends QuarkOrmEntity> entityClass, String propertyName, String primaryKey, Object primaryKeyValue, String newTempFileName, String storePath)	
	{
		if(QuarkUtilStringTools.isNullOrEmpty(newTempFileName) || QuarkUtilStringTools.isNullOrEmpty(storePath))
			return false;
		File tempFile = new File(newTempFileName);
		File newFile = new File(storePath + tempFile.getName());
		if(!tempFile.exists())
		{
			QuarkLog.logError(QuarkUiSingleUploadField.class.getName(), " temp file " + tempFile + " not exists.");
			return false;
		}
		if(!tempFile.renameTo(new File(QuarkEnvironment.getInstance().getWorkingPath() + newFile.getPath())))
			QuarkLog.log(new Exception("move file " + tempFile.getName() + " to " + QuarkEnvironment.getInstance().getWorkingPath() +  newFile.getPath() + " failed!"));
		//result.put("picture", ItcWebEnvironment.product_images + tempFile.getName());
		if(submitType.equals(QuarkUiManager.submitEdit))
		{
			QuarkDBExecuteManager manager = QuarkDBExecuterFactory.getInstance().getExecuteManager(false);
			QuarkOrmEntity entity = manager.doSelectOne(entityClass, " " + primaryKey + " = " + primaryKeyValue);
			if(primaryKeyValue == null)
			{
				QuarkLog.logError(QuarkUiSingleUploadField.class.getName(), "no such entity " + primaryKey + "= " + primaryKeyValue);
				return false;
			}
			if(!entity.getProperty(propertyName).isEmpty())
			{
				File delFile = new File(QuarkEnvironment.getInstance().getWorkingPath() + entity.getPropertyValue(propertyName).toString());
				if(delFile.exists())
					delFile.delete();
				else
				{
					QuarkLog.logError(QuarkUiSingleUploadField.class.getName(), "can't del old file " + entity.getPropertyValue(propertyName).toString());
				}
			}
		}
		return true;
	}
}
